#include "gui/gui_toolbar.h"

#include "gui/gui.h"
#include "gui/gui_controller.h"
#include "gui/gui-tool.hpp"

#include "city.h"

//////////////////////////////////////////////////
//  GuiToolbar::GuiToolbar()
//////////////////////////////////////////////////
GuiToolbar::GuiToolbar(City& city)
{
    myWindow = sfg::Window::Create(sfg::Window::Style::BACKGROUND);
    myWindow->SetTitle("Toolbar");
    myWindow->SetPosition(sf::Vector2f(0, 100));

    m_container = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 0.f );
    myWindow->Add(m_container);

    // Row1
    BuildableTool* houseTool = new BuildableTool(city);
    houseTool->setType(Buildable::RESIDENTIAL)
              .setDensity(Buildable::DENSITY_LOW)
              .setImagePath("materials/gui/tool_house.png");
    addTool(houseTool);

    BuildableTool* businessTool = new BuildableTool(city);
    businessTool->setType(Buildable::COMMERCIAL)
                 .setDensity(Buildable::DENSITY_LOW)
                 .setImagePath("materials/gui/tool_business.png");
    addTool(businessTool);

    BuildableTool* industryTool = new BuildableTool(city);
    industryTool->setType(Buildable::INDUSTRIAL)
                 .setDensity(Buildable::DENSITY_LOW)
                 .setImagePath("materials/gui/tool_industry.png");
    addTool(industryTool);

    // Row2
    BuildableTool* roadTool = new BuildableTool(city);
    roadTool->setType(Buildable::ROAD)
             .setDensity(Buildable::DENSITY_LOW)
             .setImagePath("materials/gui/tool_road.png");
    addTool(roadTool);

    BuildableTool* powerTool = new BuildableTool(city);
    powerTool->setType(Buildable::POWER_LINE)
              .setDensity(Buildable::DENSITY_LOW)
              .setImagePath("materials/gui/tool_power.png");
    addTool(powerTool);

    BuildableTool* railTool = new BuildableTool(city);
    railTool->setType(Buildable::RAIL)
             .setDensity(Buildable::DENSITY_LOW)
             .setImagePath("materials/gui/tool_rail.png");
    addTool(railTool);

    // Row 3
    BulldozerTool* bulldozerTool = new BulldozerTool(city);
    bulldozerTool->setImagePath("materials/gui/tool_bulldozer.png");
    addTool(bulldozerTool);
}

//////////////////////////////////////////////////
//  void GuiToolbar::addTool()
//////////////////////////////////////////////////
void GuiToolbar::addTool(GuiTool* t)
{
    m_tools.push_back(std::unique_ptr<GuiTool>(t));
    GuiTool& tool = *(m_tools.back().get());

    if (m_buttons.size()%3 == 0) {
      m_container->Pack(sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL, 1.f));
    }

    sfg::Widget::Ptr widget = m_container->GetChildren().back();
    sfg::Box::Ptr row = std::dynamic_pointer_cast<sfg::Box>(widget);

    sfg::Button::Ptr button = sfg::Button::Create();
    button->GetSignal(sfg::Button::OnLeftClick).Connect(std::bind(&GuiToolbar::selectTool, this, std::cref(tool)));
    button->SetImage(sfg::Image::Create(Image::Get(tool.getImagePath()).copyToImage()));
    row->Pack(button);

    m_buttons.push_back(button);
}

//////////////////////////////////////////////////
//  void GuiToolbar::selectTool()
//////////////////////////////////////////////////
void GuiToolbar::selectTool(const GuiTool& tool)
{
  GuiController::get()->setTool(tool);
}
